﻿using MaiPlanServer.IServices.IOrganizationaServices;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.Organizational
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserManageController : ControllerBase
    {
        public UserManageController(IUserManageServices Db)
        {
            this.Db = Db;
        }

        public IUserManageServices Db { get; }
        /// <summary>
        /// 分页获取用户数据
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        /// <param name="PageSize">每页显示条数</param>
        /// <param name="nick_name">用户名称</param>
        /// <param name="phonenumber">手机号码</param>
        /// <param name="status">用户状态</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int PageIndex, int PageSize, string nick_name, string phonenumber, string status)
        {
            try
            {
                var list = await Db.GetUsers(PageIndex, PageSize, nick_name,phonenumber,status);
                var count =await  Db.GetUserCount(nick_name, phonenumber, status);
                return Ok(BaseState.CreateInstance(200,"请求成功",list, count));
            }
            catch (System.Exception)
            {
                //后续写日志
            }
            return Ok(BaseState.CreateInstance(-1, "请求失败"));
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="model">用户模型</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm]sys_user model) 
        {
            try
            {
                model.user_id = Guid.NewGuid().ToString();
                model.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var result = await Db.AddAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200,"新增成功"));
                }
            }
            catch (System.Exception ex)
            {
                //后续写日志
            }

            return Ok(BaseState.CreateInstance(200, "新增失败，服务器异常"));
        }
    }
}
